package com.leedsoft.common;

import com.leedsoft.service.entity.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by leeds on 2016/11/7 16:29.
 */
public class Enums {
    public static <T> T getEnum(Class<? extends java.lang.Enum> e, Integer code) {
        return getEnum(e.getSimpleName(), code);
    }

    public static <T> T getEnum(String enumClassSimpleName, Integer code) {
        if (code == null) return null;
        try {
            Class<?> stateEnumCls = Class.forName(Enums.class.getName() + "$" + enumClassSimpleName);
            Method m = stateEnumCls.getMethod("values");
            java.lang.Enum[] ens = (java.lang.Enum[]) m.invoke(null);
            for (java.lang.Enum en : ens) {
                Method getCodeMethod = stateEnumCls.getMethod("getCode");
                if (Integer.valueOf(code).equals(getCodeMethod.invoke(en))) {
                    return (T) en;
                }
            }
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        }
        return null;
    }


    public static String getEnumName(Class<? extends java.lang.Enum> e, Integer code) {
        return getEnumName(e.getSimpleName(), code);
    }

    public static String getEnumName(String enumClassSimpleName, Integer code) {
        Enum en = getEnum(enumClassSimpleName, code);
        if (en == null) return "--";
        else try {
            return (String) en.getClass().getMethod("getName").invoke(en);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return "--";
    }

    public static <T> T[] getEntries(String enumClassSimpleName) {
        T[] ens = null;
        try {
            Class<?> stateEnumCls = Class.forName(Enums.class.getName() + "$" + enumClassSimpleName);
            Method m = stateEnumCls.getMethod("values");
            ens = (T[]) m.invoke(null);

        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        }
        return ens;
    }

    /**
     * 流程定义环节类型
     */
    public enum StepType {
        NORMAL(0, "一般环节", NormalStep.class), BRANCH(1, "分支环节", BranchStep.class),
        PARALLEL(2, "并行环节", ParallelStep.class), CONVERGE(3, "汇聚环节", ConvergeStep.class), OVER(4, "结束环节", OverStep.class);
        private Integer code;
        private String name;
        private Class<? extends Step> stepCls;

        StepType(Integer code, String name, Class<? extends Step> stepCls) {
            this.code = code;
            this.name = name;
            this.stepCls = stepCls;
        }

        public Integer getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        public Class<? extends Step> getStepCls() {
            return stepCls;
        }
    }

    /**
     * 节点类型
     */
    public enum NodeType {
        START(0, "开始事件"), TASK(1, "用户任务"), EXCLUSIVE_GATEWAY(2, "排他网关"), INCLUSIVE_GATEWAY(3, "并发网关"), OVER(4, "结束事件");
        private Integer code;
        private String name;

        NodeType(Integer code, String name) {
            this.code = code;
            this.name = name;
        }

        public Integer getCode() {
            return code;
        }

        public String getName() {
            return name;
        }
    }


    public static void main(String[] args) {
        System.out.println(Enums.getEnum(Enums.StepType.class, 1).toString());
        System.out.println(Enums.getEnumName(Enums.StepType.class, 1));
        System.out.println(Enums.getEnum("StepType", 1).toString());
        System.out.println(Enums.getEnumName("StepType", 1).toString());
    }
}
